﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Report
{
    public partial class RiskProject : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check user permission
            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);
            //if (!ReportHelper.IsAdmin(user.EmployeeId) && !ReportHelper.AllowedDownload(user.EmployeeId))
            //{

            //    this.divError.InnerHtml = AlertMessage.NoAllowedReportData; 
            //    //this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('没有相关数据权限，请联系管理员!\nNo allow to view data please contact the admin!')</script>");
            //    this.divReport.InnerHtml = "";
            //    this.btnExport.Visible = false;
            //}

            int year=0;
            int quater = 0;
            if (!Page.IsPostBack)
            {
                this.FillInReportDropdown_Adjust();

                //attach client events
                this.btnExport.Attributes.Add("onClick", "RptExportToExcel('" + ReportHelper.ReportType.RiskProject.ToString() + "');return false;");

                //initialize Year dropdown list, 5 years from current year
                BEGDC.PreContract.BLL.ICloseDate iCloseDate = (BEGDC.PreContract.BLL.ICloseDate)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_CloseDate_Service];
                DataTable dt = iCloseDate.GetCloseDateByDate(System.DateTime.Now);

                year = (int)dt.Rows[0][BEGDC.PreContract.DAL.CloseDate.CONST_Year];
                quater = (int)dt.Rows[0][BEGDC.PreContract.DAL.CloseDate.CONST_Year];
                //year = System.DateTime.Now.Year;

                //quater = ReportHelper.GetQuaterByMonth(System.DateTime.Now.Month);

                for (int i = 0; i < 5; i++)
                {
                    drYear.Items.Add(year.ToString());
                    year++;
                }
                drYear.SelectedIndex = 0;
                //set value for quater dropdown list 
                drQuater.SelectedIndex = quater - 1;

                //Run Report
                RunReport(user.EmployeeId, System.Convert.ToInt32(drYear.SelectedItem.Text), System.Convert.ToInt32(drQuater.SelectedValue), int.Parse(ddlAdjust.SelectedItem.Value));
            }
            
        }

        protected void btnRun_Click(object sender, EventArgs e)
        {
            RunReport(user.EmployeeId, System.Convert.ToInt32(drYear.SelectedItem.Text), System.Convert.ToInt32(drQuater.SelectedValue), int.Parse(ddlAdjust.SelectedItem.Value));
        }

        private void RunReport(long employeeID,int year,int quater,int adjust)
        {
            string err = string.Empty;
            string reportHTML = string.Empty;
            string excelHTML = string.Empty;
            try
            {
                reportHTML = BEGDC.PreContract.BLL.ReportHelper.GetRiskReportHTML(employeeID,
                    ReportHelper.ReportType.RiskProject, year, quater,adjust,
                    out err,
                    out excelHTML);

                if (err != string.Empty)
                {
                    this.divReport.InnerHtml = "";
                    this.divError.InnerHtml = err;
                    this.btnExport.Visible = false;
                }
                else
                {
                    err = year.ToString() + " Q" + quater.ToString();
                    divReport.InnerHtml = string.Format(reportHTML, err);
                    excelHTML = string.Format(excelHTML, err);
                    Session[ReportHelper.ReportType.RiskProject.ToString()] = excelHTML;
                    this.divError.InnerHtml = "";
                    this.btnExport.Visible = true;

                }
            }
            catch
            {
                this.divReport.InnerHtml = "";
                this.btnExport.Visible = false;
                divError.InnerHtml = AlertMessage.GetReportDataError;
                //this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + AlertMessage.GetReportDataError  + "')</script>");

            }

        }

        

        protected void btnExport_Click(object sender, EventArgs e)
        {

        }

        private void FillInReportDropdown_Adjust()
        {
            BaseFunction.BindContractAdjustDropDownList(ddlAdjust, string.Empty, true);
        }

    }
}
